package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherServicr;

import javax.lang.model.element.NestingKind;
import java.util.Scanner;

public class TeacherController {

   private TeacherServicr servicr = new TeacherServicr();
    private  Scanner s = new Scanner(System.in);
    public void start() {
        //创建循环菜单
       ll:
       while(true){
            System.out.println("欢迎使用<老师>管理系统");
            System.out.println("请输入你的选择: 1.添加老师 2. 删除老师  3.修改老师 4. 查看老师 5.退出");
            String choice = s.next();

            //判断输入的要求
            switch (choice){
                case "1" :
                    addTeacher();
                    break;
                case "2" :
                    deleteTeacher();
                    break;
                case "3" :
                    updateTeacher();
                    break;
                case "4" :
                    lookTeacher();
                    break;
                case "5" :
                    System.out.println("感谢使用老师管理系统");
                    break ll;
                default :
                    System.out.println("你输入有误,请重新输入");
                    break ;
            }
        }
    }

    private void lookTeacher() {
        //返回数组
        Teacher [] teach = servicr.lookTeacher();

        if (teach == null){
            System.out.println("暂无信息,请录入信息后查看");
            return;
        }
        System.out.println("工作ID \t + 姓名 \t + 年龄 \t\t+ 生日" );
        //遍历出 不是null的对象
        for (int i = 0; i < teach.length; i++) {
            Teacher t =teach[i];
            if (t != null){
                System.out.println(t.getId()+"\t"+t.getName()+"\t"+t.getAge()+"\t"+"\t"+t.getBirthday());
            }
        }
    }

    private void updateTeacher() {
        //当前需要使用循环语句 让其错误时重新输入
        String id =judgeId();
        Teacher newTeacher = newTeacher(id);


        //5.调用方法更换
        servicr.updateTeacher(newTeacher,id);

        //6.提示更换成功

        System.out.println("修改成功");
    }

    private void deleteTeacher() {
        String id = judgeId();
        servicr.delTeacher(id);
        System.out.println("删除成功");
    }

    private void addTeacher() {
        String id;
        while(true){
            //1.提示输入id
            System.out.println("请输入id");
            //不存在时,录入 存在时请求重新输入
            id =s.next();
            boolean exists = servicr.isExists(id);
            if (exists){
                System.out.println("学号已存在,请你重新输入");
            }else {
                break;
            }
        }
        Teacher teacher = newTeacher(id);

       boolean flag = servicr.addTeacher(teacher);
       if (flag){
       System.out.println("添加成功");
       }else {
           System.out.println("添加失败");
       }
    }

    public Teacher newTeacher(String id){
        //3. 是 提示输入姓名等.....
        System.out.println("请输入姓名");
        String name = s.next();
        System.out.println("请输入年龄");
        String age = s.next();
        System.out.println("请输入生日");
        String birthday = s.next();

        //4.将这些封装成新的一个teacher 对象
        Teacher newTeacher = new Teacher(id,name,age,birthday);

        return newTeacher;
    }

    public String judgeId(){
        String id;

        while(true){
            System.out.println("请输入id");

            id = s.next();

            boolean exists = servicr.isExists(id);
            if (!exists){
                System.out.println("学号不存在,请你重新输入");
            }else {
                break;
            }
        }
        return id;
    }
}
